package com.example.controller;

import com.example.pojo.*;
import com.example.service.CityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Pageable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/city")
public class CityController {
    @Autowired
    private CityService cityService;

    @RequestMapping("/getCityList")
    public Result getCityList(@RequestBody ConditionsQuery conditionsQuery){

        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        List<CityVo> list=cityService.getCityList(conditionsQuery);
        PageInfo<CityVo> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);
    }
    @RequestMapping("/getCityOption")
    public Result getCityOption(){
        List<CityOption> list =cityService.getCityOption();
        return new Result(200,"数据加载成功",list);
    }

    @RequestMapping("/getRegionList")
    public Result getRegionList(@RequestBody ConditionsQuery conditionsQuery){

        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        List<Region> list=cityService.getRegionList(conditionsQuery);
        PageInfo<Region> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);
    }

    @RequestMapping("/addRegion")
    public Result addRegion(@RequestBody Region region){
        return cityService.addRegion(region);
    }

    @RequestMapping("/delRegionById")
    public Result delRegionById(@RequestBody Map<String,Integer> map){

        int id =map.get("id");

        return cityService.delRegionById(id);
    }

    /**
     *
     * @param map
     * @return
     */
    @RequestMapping("/delRegionBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");

        return cityService.delRegionBatch(ids);
    }

    @RequestMapping("/getProvinceDetails")
    public Result getProvinceDetails(@RequestBody Integer id){

        System.err.println(id);
        return cityService.getProvinceDetails(id);
    }

}
